﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using ShevaEngine.Core.Modules.Animations;
using ShevaEngine.Core.Modules.Models;

namespace ShevaEngine.Core.Modules.Scenes
{
    /// <summary>
    /// Model scene object.
    /// </summary>
    public class ModelSceneObject : GraphicsSceneObject
    {
        /// <summary>Model.</summary>
        [ContentSerializerIgnore]
        public ShevaModel Model;
        /// <summary>Attached scene objects.</summary>
        [ContentSerializerIgnore]
        private List<AttachToAnimationBridge> AttachedSceneObjectBridges;

        /// <summary>
        /// Constructor.
        /// </summary>
        public ModelSceneObject()
            : base()
        {
            this.Model = null;
            this.AttachedSceneObjectBridges = new List<AttachToAnimationBridge>();            
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public ModelSceneObject(ShevaModel model)       
            : base(model)
        {            
            this.Model = model;
            this.AttachedSceneObjectBridges = new List<AttachToAnimationBridge>();

            if (this.Model.IsBoneAnimated)
                this.Model.PlayAnimation("Idle");
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public ModelSceneObject(ShevaModel model, Matrix transformation)
            : base(model, transformation)
        {
            this.Model = model;
            this.AttachedSceneObjectBridges = new List<AttachToAnimationBridge>();

            if (this.Model.IsBoneAnimated)
                this.Model.PlayAnimation("Idle");
        }

        /// <summary>
        /// Method attaches scene object to bone.
        /// </summary>
        /// <param name="sceneObject"></param>
        /// <param name="boneName"></param>
        /// <returns></returns>
        public bool AttachSceneObjectToBone(SceneObject sceneObject, string boneName)
        {
            AttachToAnimationBridge attachAnimationBridge = new AttachToAnimationBridge(sceneObject);

            this.AttachToSceneObject(attachAnimationBridge, Vector3.Zero);
            this.Transform(this.World);

            return this.Model.AttachSceneObjectToBone(attachAnimationBridge, boneName);
        }
    }
}
